public class CallScriptController {

/*
 Class Name:  CallScriptController  
 Date: 20-Jun-2011   
 Requirement/Project Name: CXG   
 Author:Manish Tiwari/Accenture IDC
 Requirement/Project Description: Agent Script Page with Issues and Sub Issue and other   
 Developer Name      Version           Updated On            Purpose
 ************************************************************************
 Manish Tiwari        v1.1             20th August 2011     Updated Logic for CSS 60 days Excluding 3 dispositions Left Message1,Ring No Answer 1,Ring No Answer 2
*/

//All the properties and variable below
Public Account acct;
public string issues{get;set;}
public string subissues{get;set;}
public string check{get;set;}
Public string noissue{get;set;}
Public string callbck{get;set;}
boolean flag=false;
boolean flag1=false;
boolean issuechk=false;
//boolean callbk=false;
boolean issueidentified=false;
Public string issueidnt{get;set;}
boolean nextlink=false;
Public Event evnt;
Public String Test1{get;set;}
Public string noissuedisp{get;set;}
Public string taskdisposition{get;set;}
Public string msgtext{get;set;}
Public string successmsgtext{get;set;}
public list<AggregateResult> lstAR = new list<AggregateResult>();
Public Id campid{get;set;}
public Id accid{get;set;}
Public Id cmid {get;set;}
Public Issue_Subissue__c isssub{get;set;} 
Public CXG_Campaign_Accounts__c campmemName{get;set;}
ID CxGTaskRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('Task', 'CXG_Task_Layout');  
public CallScriptController(ApexPages.StandardController stdController) {
        isssub =(Issue_Subissue__c )stdController.getRecord();
        accid=ApexPages.currentPage().getParameters().get('actid');
        campid=ApexPages.currentPage().getParameters().get('campname');
        cmid=ApexPages.currentPage().getParameters().get('cmid');
        campmemName=[Select Name from CXG_Campaign_Accounts__c where Id=:cmid];
      }
          
    public PageReference test() {
        return null;
    }
    List<String> udacfamily= New List<String>{'Core Listings','Limited Inventory','SEM','Website','Audio/Video','OPM','PRINT','Digital Express','Other Internet'};
   
    //Disposition Value on select of 'NO'
    public List<SelectOption> getValue() 
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--None--'));
        options.add(new SelectOption('Left Message 1','Left Message 1'));
        options.add(new SelectOption('Left Message 2','Left Message 2')); 
        options.add(new SelectOption('Ring No Answer 1','Ring No Answer 1'));
        options.add(new SelectOption('Ring No Answer 2','Ring No Answer 2'));
        options.add(new SelectOption('Ring No Answer 3','Ring No Answer 3'));
        options.add(new SelectOption('Line Disconnected / Fax Line','Line Disconnected / Fax Line'));
        options.add(new SelectOption('D2','D2'));
        return options;
    }
    
    //Disposition Value
    public List<SelectOption> getIssuedisp() 
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Customer Contacted- No Issue','Customer Contacted- No Issue'));
        return options;
    }
    
    public List<SelectOption> getIssue() 
    {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Issue_Subissue__c.Issue__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry p : ple)
        options.add(new SelectOption(p.getValue(), p.getValue()));  
        return options;
     }
    public List<SelectOption> getSubIssue() 
    {
          List<SelectOption> options = new List<SelectOption>();
          Schema.DescribeFieldResult fieldResult = Issue_Subissue__c.Subissue__c.getDescribe();
          List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
          for(Schema.PicklistEntry p : ple)
          options.add(new SelectOption(p.getValue(), p.getValue())); 
            
         return options;
    }
    public boolean getflag()
    {
      return flag;
    }
    public boolean getflag1()
    {
      return flag1;
    } 
    Public boolean getissuechk()
    {
      return issuechk;
    } 
    Public boolean getissueidentified()
    {
     return issueidentified;
    }  
    Public boolean getnextlnk()
    {
     return nextlink;
    }  
    
    
    //Action Method below on Select of Reach Decision Maker 'YES'
    public PageReference onclickaction()
    {
    if(check=='Yes')
     {
      //callbk=false;
      System.debug('starteddd');
      issueidentified=true;
      nextlink=false;
      flag1=false;
     }
    else if(check=='No')
     {
      flag=false;
      flag1=true;
      issuechk=false;
      issueidentified=false;
      //callbk=false;
      nextlink=false;

     }else{
      flag=false;
      flag1=false;
      issuechk=false;
      issueidentified=false;
      //callbk=false;
      nextlink=True;


     }
     return null;
    } 
    
    //Action Method below on Select of Reach Decision Maker 'No'
    Public void onclickactionissue()
    {
     if (issueidnt=='Yes')
     {
      issuechk=false;
      flag=True;
     }
     else
     {
      issuechk=True;
      flag=False;
     }
    }
   
    public List<SelectOption> getItems() {
          List<SelectOption> options = new List<SelectOption>(); 
          options.add(new SelectOption('Yes','Yes'));
          options.add(new SelectOption('No','No'));
          options.add(New SelectOption('Call Back Needed','Call Back Needed')); 
          return options; 
          }
    public List<SelectOption> getIssueidentfd() {
          List<SelectOption> options = new List<SelectOption>(); 
          options.add(new SelectOption('Yes','Yes'));
          options.add(new SelectOption('No','No')); 
          return options; 
          }
  Public Account getaccount()
  {
  return[Select Name,Primary_Contact_hidden__c from Account where Id=:accid];
  }
  Public Campaign getCamp()
  {
    return [Select Name,Id from Campaign where Id=:campid];
  }
  
  //Below is the process of Saving Disposition
  Public PageReference createnoissuedisp()
  {
   Task tsk1 = New Task();
   tsk1.WhatId=accid;
   tsk1.Disposition__c=noissuedisp;
   tsk1.ActivityDate=Date.Today();
   tsk1.Attempt_Date__c=DateTime.Now();
   tsk1.Status='Completed';
   tsk1.Priority='Normal';
   tsk1.Subject=noissuedisp;
   tsk1.IsContacted__c=True;
   tsk1.CXG_Account__c=True;
   tsk1.OwnerId=UserInfo.getUserId();
   tsk1.Campaign_Member_ID__c=campmemName.Name;
   tsk1.RecordTypeId=CxGTaskRecordTypeID;
   Insert tsk1;//inserting Task Under Account(Customer)
   task insrtdTask = [select Server_URL__c from task where id = :tsk1.id];
   tsk1.Campaign__c=insrtdTask.Server_URL__c+cmid;
   Update tsk1; 
   system.debug('DDDDDDDDD1'+tsk1.Server_URL__c );

   Task cmtsk = New Task();
   cmtsk.WhatId=cmid;
   cmtsk.Disposition__c=noissuedisp;
   cmtsk.ActivityDate=Date.Today();
   cmtsk.Attempt_Date__c=DateTime.Now();
   cmtsk.Status='Completed';
   cmtsk.Priority='Normal';
   cmtsk.Subject=noissuedisp;
   cmtsk.IsContacted__c=True;
   cmtsk.CXG_Account__c=True;
   cmtsk.Campaign_Member_ID__c=campmemName.Name;
   cmtsk.RecordTypeId=CxGTaskRecordTypeID;
   cmtsk.OwnerId=UserInfo.getUserId();
   cmtsk.Campaign__c=insrtdTask.Server_URL__c+cmid;
   Insert cmtsk;//Inserting Task under Campaign Members(For Reporting)
   
   //For CSS 60 Days Functionality
   Account accnt= New Account(Id=accid);
   accnt.Contacted_Timestamp__c=Date.Today();
   update accnt;
   //System.Debug('CONT'+accnt.Contacted_Timestamp__c);
   
   successmsgtext='Success-"Your disposition has been saved to the Account Record tied to this Campaign Member."';
   return null;
  }
  Public PageReference createdisp()
  {
   Task tsk= New Task();
   tsk.whatId=accid;
   tsk.Disposition__c=taskdisposition;
   tsk.ActivityDate=Date.Today();
   tsk.Attempt_Date__c=DateTime.Now();
   tsk.Status='Completed';
   tsk.Priority='Normal';
   tsk.OwnerId=UserInfo.getUserId();
   tsk.Subject=taskdisposition;
   tsk.CXG_Account__c=True;
   tsk.Campaign_Member_ID__c=campmemName.Name;
   tsk.RecordTypeId=CxGTaskRecordTypeID;
   //Event evt= new Event(Subject='Call Back Schedule',whatId=accid,StartDateTime=evnt.StartDateTime,EndDateTime=evnt.EndDateTime);
    if(taskdisposition=='None')
     {
      Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.WARNING,'Error-"Please fill the disposition before Submitting."','Error-"Please fill the Disposition before Submitting."' ));
     }
    else
     {
    Try
      {
       Insert tsk;
       task insrtdTask = [select Server_URL__c from task where id = :tsk.id];
       tsk.Campaign__c=insrtdTask.Server_URL__c +cmid;
       update tsk;
       QueueSobject hldqueueid=[Select QueueId from QueueSobject where Queue.Name ='Holding Queue']; 
       Task cmtsk = New Task();
       cmtsk.WhatId=cmid;
       cmtsk.Disposition__c=taskdisposition;
       cmtsk.ActivityDate=Date.Today();
       cmtsk.Attempt_Date__c=DateTime.Now();
       cmtsk.Status='Completed';
       cmtsk.Priority='Normal';
       cmtsk.Subject=taskdisposition;
       cmtsk.CXG_Account__c=True;
       cmtsk.Campaign_Member_ID__c=campmemName.Name;
       cmtsk.RecordTypeId=CxGTaskRecordTypeID;
       cmtsk.Campaign__c=insrtdTask.Server_URL__c +cmid;
       Insert cmtsk;//Insert Task to a Campaign members
       
       if(taskdisposition=='Left Message 1'||taskdisposition=='Ring No Answer 1'||taskdisposition=='Ring No Answer 2'){
               //Updating Owner to Holding queue
               CXG_Campaign_Accounts__c cm =New CXG_Campaign_Accounts__c(Id=cmid);
               cm.OwnerId=hldqueueid.QueueId;//Assigning Campaign Member Record to Holding Queue
               cm.AssignToQueueDate__c=Date.Today();
               update cm;
         }
        
        //Code for 60 Days CSS
        if(taskdisposition=='Left Message 2'||taskdisposition=='Ring No Answer 3'||taskdisposition=='Line Disconnected / Fax Line'||taskdisposition=='D2'){
         Account accnt= New Account(Id=accid);
         accnt.Contacted_Timestamp__c=Date.Today();
         update accnt;
         }
          
       //Permnantly retiring Customer   
       if(taskdisposition=='Line Disconnected / Fax Line'){
         Account acctnt= New Account(Id=accid);
         acctnt.Line_Disconnected_Fax_Line__c=True;
         update acctnt;
         }
        msgtext='Success-"Your disposition has been saved to the Account Record tied to this Campaign Member. Refresh the Campaign Member page to see the latest Disposition values."';
      }
    catch(Exception exe)
      {
      exe.getMessage();
      }
     }
    taskdisposition='None';
    return null;
  }
  
  //Till here is the Process of Saving Disposition
  
  // Method for dipalying Item family Matrix 
  Public CSS_Customer_Item_Families__c getMatrix()
  {
   list<CSS_Customer_Item_Families__c> CSS =[Select Core_Listings_NISD_Amount__c,Limited_Inventory_NISD_Amount__c,SEM_NISD_Amount__c,Website_NISD_Amount__c,Audio_Video_NISD_Amount__c,OPM_NISD_Amount__c,Print_NISD_Amount__c,Digital_Express_NISD_Amount__c,Other_Internet_NISD_Amount__c from CSS_Customer_Item_Families__c where Account__c=:accid];
   if(CSS.size()>0) return CSS[0];
   else return null; 
  }
   
}